<?php $total_payment = 0;?>
<?php $total_applied = 0;?>
<?php $total_unapplied = 0;?>
<table class="datagrid" style="width:300px;">
  <thead class="datagrid">
    <tr>
      <th>Payment Date</th>
      <th>Payment#</th>
      <th>Amount</th>
      <th>Applied</th>
      <th>UnApplied</th>
    </tr>
  </thead>
  <tbody class="datagrid" style="height:auto;">
    <?php foreach($cash_advances as $cash_advance):?>
    <?php 
      $payment = PaymentPeer::retrieveByPk($cash_advance['iden2']);
    ?>
    <tr>
      <td><?php echo $cash_advance['document_date'] ?></td>
      <td><?php echo $cash_advance['payment_id'] ?></td>
      <td style="text-align:right;"><?php echo number_format($payment->getCash(),2) ?></td>
      <td style="text-align:right;"><?php echo number_format($payment->getInvoiceTotal(),2) ?></td>
      <td style="text-align:right;"><?php echo number_format($cash_advance['total'],2) ?></td>
    </tr>
      <?php $total_payment += $payment->getCash();?>
      <?php $total_applied += $payment->getInvoiceTotal();?>
      <?php $total_unapplied += $cash_advance['total'];?>
    <?php endforeach;?>
  </tbody>
  <tfoot class="datagrid">
    <tr>
      <td style="text-align:right;" colspan="2">Total</td>
      <td style="text-align:right;"><?php echo number_format($total_payment,2) ?></td>
      <td style="text-align:right;"><?php echo number_format($total_applied,2) ?></td>
      <td style="text-align:right;"><?php echo number_format($total_unapplied,2) ?></td>
    </tr>
  </tfoot>
</table>